//
//  UIView+Empty.m
//  EventHouse
//
//  Created by dch on 17/7/7.
//  Copyright © 2017年 sam. All rights reserved.
//

#import "UIView+Empty.h"

@interface EmptyView : UIView
@property (nonatomic, strong) UILabel *tipsLabel;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, copy)  void(^callback)(void);
@end

@implementation EmptyView

- (instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    return self;
}

- (void)createUI
{
    self.backgroundColor = [UIColor whiteColor];
    _tipsLabel = [[UILabel alloc] init];
    _tipsLabel.font = [UIFont systemFontOfSize:13];
    _tipsLabel.textAlignment = NSTextAlignmentCenter;
    _tipsLabel.textColor = [UIColor lightGrayColor];
    [self addSubview:_tipsLabel];
    _imageView = [[UIImageView alloc] init];
    [self addSubview:_imageView];
    
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.bottom.equalTo(self.mas_centerY).offset(15);
        make.size.mas_equalTo(CGSizeMake(170, 170));
    }];
    [_tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(self.imageView.mas_bottom).offset(15);
    }];
    _imageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clicked)];
    [_imageView addGestureRecognizer:tap];
}

- (void)clicked
{
    if (_callback) {
        _callback();
    }
}

@end

@implementation UIView (Empty)

- (void)showEmptyViewWithStr:(NSString *)str image:(UIImage *)img callback:(void (^)(void))callback
{
    [self removeEmptyView];
    
    EmptyView *view = [[EmptyView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
    view.tipsLabel.text = str;
    view.imageView.image = img;
    view.callback = callback;
    [self addSubview:view];
    [view bringSubviewToFront:self];
}

- (void)removeEmptyView
{
    for (UIView *view in self.subviews) {
        if ([view isKindOfClass:[EmptyView class]]) {
            [view removeFromSuperview];
        }
    }
}

@end
